﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace project_forgotten_tuba
{
    public class Card
    {
        public int OwnerIndex { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public int ZLevel { get; set; }

        public bool Tapped { get; set; }
        public bool Flipped { get; set; }
        public bool Highlighted { get; set; }


        public Texture2D CardFront { get; set; }
        public Rectangle RectFront { get; set; }
        public Texture2D CardBack { get; set; }
        public Rectangle RectBack { get; set; }

        public Texture2D CardHighlight { get; set; }
        public Rectangle RectHighlight { get; set; }

        public string CardName { get; set; }



        public Card(string name)
        {
            CardFront = GameStatic.FileToTexture(name, "cards//");
            RectFront = CardFront.Bounds;

            CardName = name;
           


            //FileStream backfile = new FileStream("cards//cardback.png", FileMode.Open);
           // CardBack = Texture2D.FromStream(GameStatic.MainGraphicsDevice, backfile);
            //RectBack = CardBack.Bounds;
            //backfile.Close();

  
            CardBack = GameStatic.FileToTexture("cardSleeve.jpg", "cards//");
            RectBack = CardBack.Bounds;

            //FileStream highlightfile = new FileStream("cards//highlight.png", FileMode.Open);
            //CardHighlight = Texture2D.FromStream(GameStatic.MainGraphicsDevice, highlightfile);
            //RectHighlight = CardHighlight.Bounds;
            //highlightfile.Close();

            CardHighlight = GameStatic.FileToTexture("highlight.png", "cards//");
            RectHighlight = CardHighlight.Bounds;


            //FileStream highlightfile = new FileStream("cards//background.png", FileMode.Open);
            //CardHighlight = Texture2D.FromStream(GameStatic.MainGraphicsDevice, highlightfile);
            //RectHighlight = CardHighlight.Bounds;
            //highlightfile.Close();


            if (GameStatic.PlayerIndex == 1)
            {
                X = 0;
                    //GameStatic.WINDOW_WIDTH/3;
                Y = 0;
                    //GameStatic.WINDOW_HEIGHT/5;
            }

            if (GameStatic.PlayerIndex == 2)
            {
                X = (int)(GameStatic.WINDOW_HEIGHT * .80);
                Y = (int)(GameStatic.WINDOW_WIDTH*.80);
            }

        }

       



        public Rectangle GetSelectionRect()
        {
            if(!Tapped)
                return new Rectangle(X - GameStatic.CARD_WIDTH / 2, Y - GameStatic.CARD_HEIGHT / 2, GameStatic.CARD_WIDTH, GameStatic.CARD_HEIGHT);
            else
                return new Rectangle(X - GameStatic.CARD_HEIGHT / 2, Y - GameStatic.CARD_WIDTH / 2, GameStatic.CARD_HEIGHT, GameStatic.CARD_WIDTH);
        }

        public bool IsInOpponent()
        {

            Rectangle opponentrect1 = new Rectangle(0, 0, GameStatic.WINDOW_WIDTH, GameStatic.WINDOW_HEIGHT / 4);
            Rectangle opponentrect2 = new Rectangle(0, (int)(GameStatic.WINDOW_HEIGHT * 0.75), GameStatic.WINDOW_WIDTH, (int)(GameStatic.WINDOW_HEIGHT * 0.25));
            bool Opponent = false;

            Rectangle opponentrect;
            if (GameStatic.PlayerIndex == 1)
            {
                opponentrect = opponentrect1;
            }

            else
            {
                opponentrect = opponentrect2;
            }


            if (opponentrect.Contains(X, Y))
            {
                Opponent = true;
            }

            return Opponent;

        }



        public void Draw(SpriteBatch spriteBatch)
        {
            float rotation = 0;

            Texture2D maintexture = CardFront;
            Rectangle mainsrcrect = RectFront;
            Rectangle maindestrect = new Rectangle(X, Y, GameStatic.CARD_WIDTH, GameStatic.CARD_HEIGHT);






            if (Flipped || IsInOpponent())
            {
                maintexture = CardBack;
                mainsrcrect = RectBack;
            }
            if (Tapped)
            {
                rotation = (float)(Math.PI / 2);
            }


            if (Highlighted)
            {
                int border = 3;
                Texture2D bordertexture = CardHighlight;
                Rectangle borderscrect = RectHighlight;
                Rectangle borderdestrect = new Rectangle(X, Y, GameStatic.CARD_WIDTH + (border * 2), GameStatic.CARD_HEIGHT + (border * 2));


                spriteBatch.Draw(bordertexture, borderdestrect, borderscrect, Color.White, rotation, new Vector2(borderscrect.Width / 2, borderscrect.Height / 2), SpriteEffects.None, 0.0f);
            }
            

            spriteBatch.Draw(maintexture, maindestrect, mainsrcrect, Color.White, rotation, new Vector2(mainsrcrect.Width / 2, mainsrcrect.Height / 2), SpriteEffects.None, 0.0f);

        }
  


    }
}
